package com.jiesen.base.modules.company.controller;

import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jiesen.base.baseDao.RedisServiceImpl;
import com.jiesen.base.modules.company.model.JsPublicUsersGroupby;
import com.jiesen.base.modules.company.service.JsPublicUsersGroupbyService;
import com.jiesen.base.modules.login.jsLoginUser.model.JsLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.jiesen.base.modules.company.model.JsPublicUsers;
import com.jiesen.base.modules.company.service.IJsPublicUsersService;
import com.jiesen.base.utils.ImportExcelUtil;
import com.jiesen.base.utils.MultipartFileToFile;
import com.jiesen.base.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 公海用户 前端控制器
 * </p>
 *
 * @author bao
 */

@RestController
@RequestMapping(value = "/jsPublicUsers", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class JsPublicUsersController {

    @Autowired
    private IJsPublicUsersService iJsPublicUsersService;

    @Autowired
    private JsPublicUsersGroupbyService jsPublicUsersGroupbyService;

    @Autowired
    private RedisServiceImpl redisService;

    //根据条件分页查询公海用户数据
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public R getList(@RequestBody JsPublicUsers jsPublicUsers) {
        System.out.println(jsPublicUsers);
        return iJsPublicUsersService.page(jsPublicUsers);
    }

    //修改一个公海用户数据
    @RequestMapping(value = "/updJsPublicUsers", method = RequestMethod.POST)
    @ResponseBody
    public JsPublicUsers updJsPublicUsers(@RequestBody JsPublicUsers jsPublicUsers) {
        iJsPublicUsersService.updateById(jsPublicUsers);
        return jsPublicUsers;
    }

    //excel导入用户
    @RequestMapping(value = "/toExcel", method = RequestMethod.POST)
    public R getBankListByExcel(
            HttpServletRequest request,
            @RequestParam("file") MultipartFile file,
            @RequestParam("areaId")Integer areaId,
            @RequestParam("branchId")Integer branchId
            //@RequestParam("name")String name
    ) throws Exception {
        String token = request.getHeader("token");
        Object o = redisService.get(token);
        if(o==null){
            return R.error("没有登录");
        }
        JsLoginUser jsLoginUser =(JsLoginUser)o;
        File toFile = MultipartFileToFile.multipartFileToFile(file);
        FileInputStream inputStream = new FileInputStream(toFile);
        List<List<Object>> lists = ImportExcelUtil.getBankListByExcel(inputStream, toFile.getName());
        //return R.ok().put("data",list).put("areaId",areaId).put("branchId",branchId).put("name",name);
        ArrayList<JsPublicUsers> arrayList = new ArrayList<JsPublicUsers>();
        ArrayList<JsPublicUsers> arrayList1 = new ArrayList<JsPublicUsers>();
        String name = toFile.getName();

        Date date = new Date();
        JsPublicUsersGroupby jsPublicUsersGroupby = new JsPublicUsersGroupby();
        jsPublicUsersGroupby.setBranchId(branchId);
        jsPublicUsersGroupby.setLock(1);
        jsPublicUsersGroupby.setCts(date);
        DateFormat format = new SimpleDateFormat("yyyy年MM月dd日-HH时mm分ss秒");
        String format1 = format.format(date);
        jsPublicUsersGroupby.setName(format1+"-"+jsLoginUser.getName());
        jsPublicUsersGroupbyService.save(jsPublicUsersGroupby);

        Integer grouupId = jsPublicUsersGroupby.getId();

        for (int i = 0; i < lists.size(); i++) {

            List list = lists.get(i);
            if(list.size()<=5){
                for(int n = list.size();n<=5;n++){
                    list.add(null);
                }
            }

            JsPublicUsers publicUsers = new JsPublicUsers();
            //名称
            if(list.get(0)==null){
                continue;
            }
            publicUsers.setName(list.get(0).toString());

            //电话
            if(list.get(1)==null){
                continue;
            }
            String s = list.get(1).toString();
            if (s.length() > 11) {
                publicUsers.setTelephone(s.substring(0, 11));
            } else {
                publicUsers.setTelephone(s);
            }
            //地址
            if(list.get(2)!=null) {
                publicUsers.setAddress(list.get(2).toString());
            }
            //备注
            if(list.get(3)!=null)
            publicUsers.setRemarks(list.get(3).toString());
            //意向等级
            if(list.get(4)!=null) {
            }
            //创建时间
            publicUsers.setCts(new Date());
            //类型
            publicUsers.setState(0);
            publicUsers.setBranchId(branchId);
            publicUsers.setAreaId(areaId);
            publicUsers.setGroupbyId(grouupId);
            arrayList.add(publicUsers);
            if (arrayList.size() == 200) {
                List<JsPublicUsers> jsPublicUsers = iJsPublicUsersService.addList(arrayList);
                arrayList1.addAll(jsPublicUsers);
                arrayList.clear();
            }
        }
        if(arrayList.size()>0) {
            List<JsPublicUsers> jsPublicUsers = iJsPublicUsersService.addList(arrayList);
            arrayList1.addAll(jsPublicUsers);
        }
        //toFile.delete();
        return R.ok().put("error",arrayList1);
    }

    //增加一个公海用户
    @RequestMapping(value = "/addJsPublicUsers", method = RequestMethod.POST)
    @ResponseBody
    public JsPublicUsers addJsPublicUsers(@RequestBody JsPublicUsers jsPublicUsers) {
        iJsPublicUsersService.save(jsPublicUsers);
        return jsPublicUsers;
    }

}
